(function () {
    return {
        import: [{url: "js/websocket.js"}, {url: "css/code.auto.css"}],
        init:function(){
            var _this = this;
            var data = $.cookie(_this.modulePath + "data");
            if (data) {
                _this.data = JSON.parse(data);
            }
            if (!_this.data.nodePort) {
                _this.data.nodePort = 1212;
            }
            if (!_this.data.port) {
                _this.data.port = 3306;
            }
            _this.data.tables = {};
        },
        before: function (data) {
           return data;
        },
        after: function () {
            var _this = this;
            _this.$(".e-connection").click(function () {
                if (!_this.$(".data-form").validate().form()) {
                    return;
                }
                var dataCache = _this.toMap(".data-param");
                $.cookie(_this.modulePath + "data", JSON.stringify(dataCache), {path: "/", expires: 365});
                var ws = new WebSocketUtil("ws://127.0.0.1:" + dataCache.nodePort);
                ws.noConnectionFun = function () {
                    toastr.error("无法连接到mysql，请检测本地是否开启node服务");
                }
                ws.handler["getAllTableName"] = function (data) {
                    _this.data.tables = data;
                    ws.ws.close();
                    _this.reload();
                }
                ws.sendJsonStr({
                    code: "getAllTableName",
                    data: {
                        user: dataCache.user,
                        host: dataCache.host,
                        password: dataCache.password,
                        port: dataCache.port,
                        database: dataCache.database
                    }
                });
            });

            _this.$(".e-download-node").click(function () {
                window.open(window.root + "download/node-ds-util.zip");
            });
            _this.$(".e-getCreateTable").click(function () {
                if (!_this.$(".data-form").validate().form()) {
                    return;
                }
                var tableName = $(this).attr("data");
                var dataCache = _this.toMap(".data-param");
                $.cookie(_this.modulePath + "data", JSON.stringify(dataCache), {path: "/", expires: 365});
                var ws = new WebSocketUtil("ws://127.0.0.1:" + dataCache.nodePort);
                ws.noConnectionFun = function () {
                    toastr.error("无法连接到mysql，请检测本地是否开启node服务");
                }
                ws.handler["getCreateTableSql"] = function (data) {
                    ws.ws.close();
                    var m = DsModule.getModule("main");
                    if (m != null) {
                        m.remove();
                    }
                    m = new Module("module/tableCreateCode/index");
                    m.params.sql = data;
                    m.params.hideBtn = true;
                    m.load("main");

                }
                ws.sendJsonStr({
                    code: "getCreateTableSql",
                    data: {
                        user: dataCache.user,
                        host: dataCache.host,
                        password: dataCache.password,
                        port: dataCache.port,
                        database: dataCache.database,
                        tableName: tableName
                    }
                });
            })
        }
    }
});